package com.example.exam.aspect;

import com.example.exam.logInfo.model.LogInfoBean;
import com.example.exam.logInfo.service.LogInfoService;
import com.example.exam.utils.IpUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Aspect
@Component
public class LogAspect {
    @Autowired
    LogInfoService lis;
    @Pointcut("execution(public * com.example.exam.logInfo.controller.*.*(..))"+
            "||execution(public * com.example.exam.sysDictInfo.controller.*.*(..))"+
            "||execution(public * com.example.exam.user.controller.*.*(..))"+
            "||execution(public * com.example.exam.sysRole.controller.*.*(..))"+
            "||execution(public * com.example.exam.sysResource.controller.*.*(..))"+
            "||execution(public * com.example.exam.post.controller.*.*(..))"+
            "||execution(public * com.example.exam.staff.controller.*.*(..))"
    )
    public void webLog(){}
    @After("webLog()")
        /** 
            * @Description:  后置最终通知,final增强，不管是抛出异常或者正常退出都会执行插入日志
            * @Param: [joinPoint] 
            * @return: void 
            * @Author: xhw
            * @Date: 2021/7/26 
            */
    public void after(JoinPoint joinPoint){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        LogInfoBean lib=new LogInfoBean();
        lib.setOp_time(new Date());
        lib.setStatus(0);
        lib.setUrl(request.getRequestURL().toString());
        lib.setIp(IpUtil.getIpAddr(request));
        String modelName=request.getRequestURL().toString().split("/")[3];
        if(modelName.equals("user")){
            modelName="用户管理";
        }else if (modelName.equals("logInfoController")){
            modelName="系统日志";
        }else if (modelName.equals("sysDictInfoController")){
            modelName="数据字典";
        }else if (modelName.equals("sysRoleController")){
            modelName="角色管理";
        }else if(modelName.equals("sysResource")){
            modelName="菜单管理";
        }else if(modelName.equals("dtree")){
            modelName="菜单管理";
        }else if(modelName.equals("staff")){
            modelName="员工管理";
        }else if(modelName.equals("post")){
            modelName="岗位管理";
        }
        lib.setModle_name(modelName);
        String methodName=joinPoint.getSignature().getName();
        if("toUser".equals(methodName)||"toLog".equals(methodName)||"toLogin".equals(methodName)||"login".equals(methodName)||"list".equals(methodName)||
                "toSysRole".equals(methodName)||"selectRole".equals(methodName)||"toSysResource".equals(methodName)||"toResourceList".equals(methodName)
                ||"toParent".equals(methodName)||"toSysDictInfo".equals(methodName)||"selectDict".equals(methodName)||"findParentDict".equals(methodName)
                ||"queryParentResource".equals(methodName)||"queryResource".equals(methodName)||"toDtreePage".equals(methodName)||"toMain".equals(methodName)||
                "toMain".equals(methodName)||"toPost".equals(methodName)||"toStaff".equals(methodName)||"toMyself".equals(methodName)||"head".equals(methodName)){
            methodName="查看";
        }else if ("toAddUser".equals(methodName)||"insertUser".equals(methodName)||"toAddRole".equals(methodName)||"insertRole".equals(methodName)||
                "toAddResource".equals(methodName)||"insertResource".equals(methodName)||"toAddDict".equals(methodName)||"insertDict".equals(methodName)||"add".equals(methodName)
                ||"toAdd".equals(methodName)){
            methodName="添加";
        }else if ("updateUser".equals(methodName)||"toUpdate".equals(methodName)||"selectOne".equals(methodName)||"productPwd".equals(methodName)||"toUpdateRole".equals(methodName)||
                "updateRole".equals(methodName)||"toEditResource".equals(methodName)||"updateResource".equals(methodName)||"toUpdateDict".equals(methodName)||
                "updateDict".equals(methodName)||"selectOneDict".equals(methodName)||"edit".equals(methodName)||"toEdit".equals(methodName)||"editPwd".equals(methodName)||"toEditPwd".equals(methodName)||"restPwd".equals(methodName)){
            methodName="修改";
        }else if ("deleteUser".equals(methodName)||"toDelete".equals(methodName)||"deleteRole".equals(methodName)||
                "deleteResource".equals(methodName)||"deleteDict".equals(methodName)||"delete".equals(methodName)||"toDelete".equals(methodName)){
            methodName="删除";
        }else if ("toGrant".equals(methodName)||"grant".equals(methodName)||"index".equals(methodName)||"getTreeList".equals(methodName)||"savePersmission".equals(methodName)||"saveGrant".equals(methodName)){
            methodName="账号设置";
        }
        lib.setMethod_name(methodName);
        HttpSession session=request.getSession();
        Integer id=(Integer) session.getAttribute("id");
        lib.setUser_id(id);
        lib.setExpend(110);
        lib.setData_id("1");
        lib.setOper_style(request.getMethod());
        if (id!=null){
            lis.insertLog(lib);
        }
    }
}
